package com.ip;

import com.graphics.ImageIdentity;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import mapgenerator.*;

/**
 *
 * @author Sajarwo Anggai
 */
public class IPControlPanel implements ActionListener, ChangeListener, TreeSelectionListener {

    private Main comp;
    private ImageIdentity currentImageIdentity;
    private String currentImageName;
    private int hGap = 5;
    private int vGap = 5;

    public IPControlPanel(Main comp) {
        this.comp = comp;
        comp.IPButtonRename.addActionListener(this);
        comp.IPButtonRemove.addActionListener(this);
        comp.IPButtonSetOnWorkspace.addActionListener(this);
        comp.IPHGapSlider.addChangeListener(this);
        comp.IPVGapSlider.addChangeListener(this);
    }

    void showImgeManagerInfo(boolean active) {
        comp.IPButtonRename.setVisible(active);
        comp.IPButtonRemove.setVisible(active);
        comp.IPButtonSetOnWorkspace.setVisible(active);
    }

    public void setHGap(int hGap) {
        this.hGap = hGap;
    }

    public void setVGap(int vGap) {
        this.vGap = vGap;
    }

    public int getHGap() {
        return hGap;
    }

    public int getVGap() {
        return vGap;
    }

    public void setCurrentImageIdentity(ImageIdentity currentImageIdentity) {
        this.currentImageIdentity = currentImageIdentity;
    }

    public void stateChanged(ChangeEvent e) {
        if (e.getSource() == comp.IPHGapSlider) {
            setHGap(comp.IPHGapSlider.getValue());
            comp.IPHGapSlider.setToolTipText("" + comp.IPHGapSlider.getValue());
            comp.IPHGap.setText("HGap: " + comp.IPHGapSlider.getValue() + "px");
        } else {
            setVGap(comp.IPVGapSlider.getValue());
            comp.IPVGapSlider.setToolTipText("" + comp.IPVGapSlider.getValue());
            comp.IPVGap.setText("VGap: " + comp.IPVGapSlider.getValue() + "px");
        }
        if (!comp.project.isCreated()) {
            return;
        }
        previewPattern(comp.ImagePatternRightPanelRender, currentImageIdentity.getName());

    }

    public void setCurrentImage(String currentImageName) {
        this.currentImageName = currentImageName;
    }

    public String getCurrentImage() {
        return currentImageName;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == comp.IPButtonRename) {
            comp.imControlPanel.renameImage(currentImageName);
        } else if (e.getSource() == comp.IPButtonRemove) {
            removeImagePattern();
        } else {
            previewPattern(comp.WorkspaceBottom, currentImageIdentity.getName());
        }
    }

    public void removeImagePattern() {
        comp.ipContainer.remove(getCurrentImage());
        comp.ipPanelWidget.reloadAll();
        comp.osPanelWidget.reloadAll();
    }

    public void valueChanged(TreeSelectionEvent e) {
        if (!comp.project.isCreated()) {
            return;
        }
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) comp.ipPanelWidget.getTree().getLastSelectedPathComponent();
        try {
            if (node != null & !node.isRoot() & node.isLeaf()) {
                ImageIdentity ii = (ImageIdentity) node.getUserObject();
                previewPattern(comp.ImagePatternRightPanelRender, ii.getName());
                showImgeManagerInfo(true);
                setCurrentImageIdentity(ii);
                setCurrentImage(ii.getName());
            } else {
                showImgeManagerInfo(false);
                comp.ImagePatternRightPanelRender.removeAll();
            }
        } catch (Exception err) {
            showImgeManagerInfo(false);
            comp.ImagePatternRightPanelRender.removeAll();
        }
        comp.ImagePatternRightPanelRender.repaint();
        comp.ImagePatternRightPanelRender.revalidate();
    }

    public void previewPattern(JPanel render, String imageName) {
        ImageIdentity ident = comp.ipContainer.getPattern(imageName);
        if (ident == null) {
            return;
        }
        if (render == comp.ImagePatternRightPanelRender) {
            setCurrentImageIdentity(ident);
        }
        render.removeAll();
        int x = 0, y = 0;
        int width = 0, height = 0;
        for (int i = 0; i < ident.getImages().size(); i++) {
            TilePattern tp = new TilePattern("", (BufferedImage) ident.getImages().get(i), i);

            tp.setVisible(true);
            if (ident.isSprite()) {
                width = tp.getImage().getWidth();
                height = tp.getImage().getHeight();
            } else {
                width = comp.project.getTileWidth();
                height = comp.project.getTileHeight();
            }
            render.add(tp).setBounds(x, y, width, height);
            if ((i + 1) % ident.getCols() == 0) {
                x = 0;
                y += height + comp.ipControlPanel.getVGap();
            } else {
                x += width + comp.ipControlPanel.getHGap();
            }
        }
        x = ident.getCols() * width;
        x += ident.getCols() * comp.ipControlPanel.getHGap();
        y = ident.getRows() * height;
        y += ident.getRows() * comp.ipControlPanel.getVGap();

        render.setPreferredSize(new Dimension(x, y));
        render.repaint();
        render.revalidate();
    }
}
